<template>
  <view class="upload-container">
    <!-- 照片预览区域 -->
    <view class="cover-section">
      <view class="section-title">照片预览</view>
      <view class="cover-upload">
        <image 
          v-if="photoUrl" 
          :src="photoUrl" 
          mode="aspectFill" 
          class="cover-image"
          @load="onImageLoaded"
          @error="handleImageError"
        ></image>
      </view>
    </view>
    
    <!-- 表单区域 -->
    <view class="form-section">
      <view class="form-group">
        <text class="form-label">照片标题</text>
        <input 
          class="form-input" 
          v-model="form.title" 
          placeholder="请输入照片标题" 
          maxlength="50"
        />
      </view>
      
      <view class="form-group">
        <text class="form-label">照片描述</text>
        <textarea 
          class="form-textarea" 
          v-model="form.description" 
          placeholder="请输入照片描述..." 
          maxlength="200"
        ></textarea>
        <text class="word-count">{{form.description.length}}/200</text>
      </view>
      
      <view class="form-group">
        <text class="form-label">访问权限</text>
        <view class="switch-container">
          <text>设为私密照片</text>
          <switch 
            :checked="form.status === 0" 
            @change="onPrivacyChange" 
            color="#3a8838"
          />
        </view>
      </view>
    </view>
    
    <!-- 底部按钮 -->
    <view class="bottom-bar">
      <button class="cancel-btn" @click="goBack">取消</button>
      <button 
        class="upload-btn" 
        :disabled="isSaving"
        @click="saveChanges"
      >{{ isSaving ? '保存中...' : '保存修改' }}</button>
    </view>
  </view>
</template>

<script>
import { getPhotoDetail, updatePhoto } from '@/api/photo'
import { getImageBlob } from '@/utils/image.js'

export default {
  navigationBarTitleText: '编辑照片',
  navigationStyle: 'default',
  backgroundColor: '#f8f8f8',
  navigationBarButtonStyle: 'black',
  data() {
    return {
      photoId: '',
      photoUrl: '',
      form: {
        title: '',
        description: '',
        status: 1, // 1-公开，0-私密
      },
      isSaving: false,
      imageLoaded: false
    }
  },
  onLoad(options) {
    // 获取照片ID
    this.photoId = options.id
    if (!this.photoId) {
      uni.showToast({
        title: '参数错误',
        icon: 'none'
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 1500)
      return
    }
    
    // 加载照片信息
    this.loadPhotoDetail()
  },
  methods: {
    // 加载照片详情
    async loadPhotoDetail() {
      try {
        const res = await getPhotoDetail(this.photoId)
        if (res.data) {
          const photo = res.data
          
          // 设置表单数据
          this.form.title = photo.title || ''
          this.form.description = photo.description || ''
          this.form.status = photo.status
          
          // 加载照片
          if (photo.photoPath) {
            try {
              const photoUrl = await getImageBlob(photo.photoPath)
              if (photoUrl) {
                this.photoUrl = photoUrl
              }
            } catch (err) {
              console.error('加载照片失败:', err)
              uni.showToast({
                title: '加载照片失败',
                icon: 'none'
              })
            }
          }
        }
      } catch (error) {
        console.error('加载照片详情失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    },
    
    // 图片加载完成
    onImageLoaded() {
      console.log('图片加载完成')
      this.imageLoaded = true
    },
    
    // 图片加载失败
    handleImageError(e) {
      console.error('图片加载错误:', e)
      uni.showToast({
        title: '图片加载失败',
        icon: 'none'
      })
    },
    
    // 切换私密状态
    onPrivacyChange(e) {
      this.form.status = e.detail.value ? 0 : 1
    },
    
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    
    // 保存修改
    async saveChanges() {
      if (this.isSaving) return
      
      // 表单验证
      if (!this.form.title.trim()) {
        uni.showToast({
          title: '请输入照片标题',
          icon: 'none'
        })
        return
      }
      
      this.isSaving = true
      try {
        await updatePhoto(this.photoId, {
          title: this.form.title.trim(),
          description: this.form.description.trim(),
          status: this.form.status
        })
        
        uni.showToast({
          title: '保存成功',
          icon: 'success'
        })
        
        // 发送编辑成功事件
        uni.$emit('photo:updated', { photoId: this.photoId })
        
        // 延迟返回
        setTimeout(() => {
          uni.navigateBack({
            delta: 1,
            success: () => {
              // 确保返回后页面已刷新
              uni.$emit('photo:updated', { photoId: this.photoId })
            }
          })
        }, 1500)
      } catch (error) {
        console.error('保存失败:', error)
        uni.showToast({
          title: error.message || '保存失败',
          icon: 'none'
        })
      } finally {
        this.isSaving = false
      }
    }
  }
}
</script>

<style>
.upload-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  display: flex;
  flex-direction: column;
}

/* 封面区域样式 */
.cover-section {
  background-color: #fff;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 28rpx;
  color: #666;
  padding: 30rpx;
}

.cover-upload {
  margin: 0 30rpx 30rpx;
  height: 400rpx;
  background-color: #f5f5f5;
  border-radius: 12rpx;
  overflow: hidden;
  position: relative;
}

.cover-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.form-section {
  flex: 1;
  padding: 30rpx;
}

.form-group {
  margin-bottom: 30rpx;
  position: relative;
}

.form-label {
  display: block;
  font-size: 28rpx;
  color: #666;
  margin-bottom: 15rpx;
}

.form-input {
  width: 100%;
  height: 80rpx;
  background-color: #fff;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
  box-sizing: border-box;
}

.form-textarea {
  width: 100%;
  height: 200rpx;
  background-color: #fff;
  border-radius: 8rpx;
  padding: 20rpx;
  font-size: 28rpx;
  box-sizing: border-box;
}

.word-count {
  position: absolute;
  bottom: 10rpx;
  right: 20rpx;
  font-size: 24rpx;
  color: #999;
}

.switch-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx;
  background-color: #fff;
  border-radius: 8rpx;
  font-size: 28rpx;
}

.bottom-bar {
  padding: 30rpx;
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  border-top: 1rpx solid #eee;
}

.upload-btn, .cancel-btn {
  width: 45%;
  height: 80rpx;
  line-height: 80rpx;
  text-align: center;
  border-radius: 40rpx;
  font-size: 28rpx;
}

.cancel-btn {
  background-color: #f5f5f5;
  color: #666;
}

.upload-btn {
  background-color: #3a8838;
  color: #fff;
}

.upload-btn[disabled] {
  background-color: #cccccc;
  color: #ffffff;
}
</style> 